using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace SunshineFarmWord
{
    public class PlayerRunState : PlayerBaseStates
    {
        public PlayerRunState(PlayerStateMachine playerStateMachine) : base(playerStateMachine)
        {
        }

        #region base function
        public override void Enter()
        {
            base.Enter();

            InitReusableData();

            StartAnimation(stateMachine.Player.AnimationData.RunParameterHash);
        }

        public override void Exit()
        {
            base.Exit();

            ClearResableData();

            StopAnimation(stateMachine.Player.AnimationData.RunParameterHash);
        }

        public override void PhysicsUpdate()
        {
            base.PhysicsUpdate();
            Move();
        }
        #endregion

        #region Main function
        // 移动
        protected override void Move()
        {
            // 内部处理了移动方法 这里判断是否需要移动即可
            if (!stateMachine.ReusableData.IsRunState) return;
            base.Move();
        }
        // 初始化数据
        private void InitReusableData()
        {
            stateMachine.ReusableData.IsWalkState = false;
            stateMachine.ReusableData.IsRunState = true;

            stateMachine.ReusableData.MovementSpeedModifier = movementData.RunBaseSpeed;
        }

        // 清理数据
        private void ClearResableData()
        {
            stateMachine.ReusableData.IsRunState = false;
        }
        #endregion

        #region 按键事件监听
        // 取消移动
        protected override void OnMovementCanceled(InputAction.CallbackContext context)
        {
            base.OnMovementCanceled(context);
            //Debug.Log("取消移动-----");
            ClearResableData();
            stateMachine.ChangeState(stateMachine.IdlingState);
        }

        // shift 事件
        protected override void OnAccelerateStarted(InputAction.CallbackContext context)
        {
            base.OnAccelerateStarted(context);

            ClearResableData();

            stateMachine.ChangeState(stateMachine.WalkState);
        }
        #endregion
    }
}
